package com.westcom.vulb.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.westcom.vulb.activity.R;
import com.westcom.vulb.application.GlobalContext;

public class Time {
    public static SimpleDateFormat DEFAULT_PARSER = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    
    public static String formatTime(String time) {
        return formatTime(time, DEFAULT_PARSER);
    }
    
    public static String formatTime(String time, SimpleDateFormat parser) {
        String formatTime = time;
        
        try {
            long unixTime = parser.parse(time).getTime();
            Date date = new Date(unixTime);
            
            if (unixTime > todayTime()) {
                String todayStr = GlobalContext.getInstance().getContext().getString(R.string.today);
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                formatTime = todayStr + " " + sdf.format(date);
            } else if (unixTime > thisYearTime()) {
                SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
                formatTime = sdf.format(date);
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                formatTime = sdf.format(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        return formatTime;
    }
    
    public static long todayTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }
    
    public static long thisYearTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), 0, 0, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }
}